package com.zk.controller;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.zookeeper.KeeperException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zk.ZooKeeperProSync;
import com.zk.service.MqSendService;

@RestController
public class ZkController {

	
	@Autowired
	ZooKeeperProSync zookeeperProSync;
	
	//推送服务
	@Autowired
	MqSendService mqSendService;
	
	
	@RequestMapping("hi")
	public String getHello(String name) throws InterruptedException, IOException, KeeperException {
		System.err.println("*******************开始****************");
		zookeeperProSync.getZk(name);
		System.err.println("*******************结束*****************");
		return "你好："+name;
	}
	
	//@Autowired
	//ZooKeeper zk;
	
	@RequestMapping("cretZk")
	public String createZk(String path,String data) throws InterruptedException, IOException, KeeperException {
		System.err.println("*******************开始****************");
	     
		zookeeperProSync.createZk(path,data);
		System.err.println("*******************结束*****************");
		return "创建成功";
	}
	@RequestMapping("updateZk")
	public String updateZk(String path,String data) throws InterruptedException, IOException, KeeperException {
		System.err.println("*******************开始****************");
	     
		zookeeperProSync.updateNode(path,data);
		System.err.println("*******************结束*****************");
		return "创建成功";
	}
	@RequestMapping("/delete")
    public String delete(@RequestParam String path) {
        try {
        	zookeeperProSync.delete(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
	@RequestMapping("/getzk")
    public String exists(@RequestParam String path) {
        try {
        	System.err.println("***"+zookeeperProSync.exists(path,false));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
	
	@RequestMapping("/getData")
	public String getData(@RequestParam String path) {
		
		System.err.println("***"+zookeeperProSync.getData(path ));
		return "success";
	}
	
	/**
	 * 测试推送
	 */
	@RequestMapping("/pushMsg")
	public String pushMessage() {
		Map<String,Object> map = new HashMap<>();
		String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "接口消息推送, 你好!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        String str = mqSendService.SendMessage(map);
        
        return str;
	}
	/**
	 * 测试推送
	 */
	@RequestMapping("/pushTopicMsg")
	public String pushTopicMessage() {
		Map<String,Object> map = new HashMap<>();
		String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "topic接口消息推送, 你好!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        String str = mqSendService.SendMessage(map);
        
        return str;
	}
	/**
	 * 测试推送
	 */
	@RequestMapping("/pushFaoutMsg")
	public String pushFaoutMessage() {
		Map<String,Object> map = new HashMap<>();
		String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "Faout接口消息推送, 你好!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        String str = mqSendService.SendMessage(map);
        
        return str;
	}
}
